package cn.edu.ntu.yyl.service.imlp;

import cn.edu.ntu.yyl.dao.UserInfoDao;
import cn.edu.ntu.yyl.pojo.article.Article;
import cn.edu.ntu.yyl.pojo.article.ArticleComment;
import cn.edu.ntu.yyl.pojo.user.Role;
import cn.edu.ntu.yyl.pojo.user.UserInfo;
import cn.edu.ntu.yyl.service.UserActionService;
import cn.edu.ntu.yyl.tool.Md5Tool;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserActionServiceImpl implements UserActionService {
	@Autowired
	UserInfoDao userInfoDao;
	Logger logger;
	//添加新用户(用户注册)
	public int addnewuser(UserInfo userInfo) {
		//获取最大ID
		Integer id = getmaxid();
		if (id<0) {id=1;}
	 	//设置用户ID
		userInfo.setId(id);
		//加密用户密码
		userInfo.setPassword(Md5Tool.code(userInfo.getPassword()));
	 	try {

			 if (checkNameandemail(userInfo)>=1) {
			 	//获取当前时间
			 	Date date=new Date(System.currentTimeMillis());
				 userInfo.setCreated(date);
				 //添加用户
				 userInfoDao.addUser(userInfo);
			}
        } catch (Exception e) {
			System.err.println(e.toString());
			return -1;
        }
		return 1;
	}
	//更改用户信息
	public int update(UserInfo userInfo) {
		try {
			userInfoDao.updateUser(userInfo);
		} catch (Exception e) {
			return -1;
		}
		return 1;
	}
	//用户登录
	public UserInfo login(UserInfo loginInfo) {
		//初始化
		UserInfo user;
		//先按照用户名进行查询
		if(loginByUserNameAndPassWord(loginInfo)!=null)
		{
			user = loginByUserNameAndPassWord(loginInfo);
			if(user!=null&&updatelogintime(user)>=1){
				return user;
			}
		}
		else 
		{
			//用户名查询不到,再对邮箱进行查询
			if(loginByEmail(loginInfo)!=null)
			{
				user = loginByEmail(loginInfo);
				if(user!=null&&updatelogintime(user)>=1){
					return user;
				}
			}
		}
		//登录失败
		return null;
	}
	//用户注销
	public int deleteUser(UserInfo userInfo) {
		try {
			userInfoDao.deleteUser(userInfo);
		} catch (Exception e) {
			return -1;
		}
		return 1;
	}
	//删除用户
	public int deleteUser(Integer id) {
		try {
			userInfoDao.deleteUserById(id);
		} catch (Exception e) {
			return -1;
		}
		return 1;
	}
	//查询所有用户
	public List<UserInfo> getUserlist() {
		try {
			return userInfoDao.getUserInfoList();
		} catch (Exception e) {
			System.err.println(e.toString());
			return null;
		}
	}
	//根据ID查询用户
	public UserInfo getUserInfoByID(Integer ID){
		try {
			return userInfoDao.getUserInfoByID(ID);
		} catch (Exception e) {
			System.err.println(e.toString());
			return null;
		}
	}
	//根据用户名查询用户
	public UserInfo getUserByUserName(String username){
		try {
			return userInfoDao.getUserByUserName(username);
		} catch (Exception e) {
			System.err.println(e.toString());
			return null;
		} 
	}
	//根据文章查询用户
	public UserInfo SelectUserByArticle(Article article){
		try {
			return userInfoDao.SelectUserByArticle(article);
		} catch (Exception e) {
			System.err.println(e.toString());
			return null;
		} 
	}
	//查询权限组的所有用户
	public List<UserInfo> SelectByRole(Role role){
		try {
			return userInfoDao.SelectByRole(role);
		} catch (Exception e) {
			System.err.println(e.toString());
			return null;
		} 
	}
	//按照评论查询用户
	public UserInfo SelectByComment(ArticleComment articleComment){
		try {
			return userInfoDao.SelectByComment(articleComment);
		} catch (Exception e) {
			System.err.println(e.toString());
			return null;
		} 
	}
	//检查是否重名
	private Integer checkName (UserInfo userInfo) {

		List<UserInfo> userInfos =  userInfoDao.getUserInfoList();

		String username = userInfo.getUsername();
		//逐个排查是否重名
		for (UserInfo info : userInfos) {
			//如果有相等的返回-1
			if (username.equals(info.getUsername())) {
				return -1;
			}
		}
		return 1;
	}
	//检查是否重复邮箱
	private Integer checkEmail(UserInfo userInfo) {

		List<UserInfo> userInfos =  userInfoDao.getUserInfoList();

 		String email = userInfo.getEmail();
		//逐个排查是否重邮箱
		for (UserInfo info : userInfos) {
			//如果有相等的返回-2
			if (email.equals(info.getEmail())) {
				return -2;
			}
		}
	 		return 1;	
	}
	//检查是否重复邮箱和重名
	private Integer checkNameandemail(UserInfo userInfo) {
		Integer result ;
		result = checkName(userInfo);
		if (result<=0) {return result;}
		result = checkEmail(userInfo);
		return result;
	}
	//获取用户最大ID
	private Integer getmaxid() {
		//初始化
		int useridl = -1;
		List<UserInfo> userInfos = null;
	 	try {
		 	userInfos = userInfoDao.getUserInfoList();
	    } catch (Exception e) {
			System.err.println(e.toString());
	 		logger.error(e.toString());
	    }
	 	//获取最大ID然后+1便是需要写入的userID
		if (userInfos!=null){

			for (UserInfo userInfo : userInfos) {
				if (useridl < userInfo.getId()) {
					useridl = userInfo.getId();
				}
			}
		} else return -1;
	 	useridl++;
	 	return useridl;
	 	
	}
	//用户登录(通过邮箱和密码登录)
	private UserInfo loginByEmail(UserInfo loginInfo) {
		//取得SqlSession对象(openSession)
		try {
			UserInfo userinfo = userInfoDao.getUserByEmailAndPassWord(loginInfo);
			//如果有返回值则表示查询成功
			if (userinfo != null) {
				return userinfo;
			}
		} catch (Exception e) {
			logger.error(e.toString());
			System.err.println(e.toString());
		}
		//关闭连接
		return null;
	}
	//用户登录(通过用户名和密码登录)
	private UserInfo loginByUserNameAndPassWord(UserInfo loginInfo) {
		//取得SqlSession对象(openSession)
		try  {
			//getMapper
			
			//执行SQL
			UserInfo userinfo = userInfoDao.getUserByUserNameAndPassWord(loginInfo);
			//如果有返回值则表示查询成功
			if (userinfo != null) {
				return userinfo;
			}
		} catch (Exception e) {
			logger.error(e.toString());
			System.err.println(e.toString());
		}
		//关闭连接
		return null;
	}
	//登录成功之后更新登录时间
	private int updatelogintime(UserInfo userInfo) {
	 		Date date = new Date(System.currentTimeMillis());
			userInfo.setLogged(date);
	 		//取得SqlSession对象(openSession)
	 		
	 		try {
			 	userInfoDao.updatelogintime(userInfo);
	        } catch (Exception e) {
	            System.err.println(e.toString());
				return -1;
	        }
	 		return  1;
		}






}